﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Text;
using System.Runtime.InteropServices;

using StarMicronics.StarIO;
using System.Threading;
using System.Windows.Media.Imaging;
using System.Drawing.Printing;
using AddUserLib;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Windows;
using AddProductLib;

namespace MicroStarPrinter
{
    public static class Printer
    {
        public static void testPrint()
        {
            List<NovyZbozi> zboziList = new List<NovyZbozi>();
            //zboziList.Add(new NovyZbozi("65465DDSSD", "nazev", 545, DateTime.Now, "5", 5, DateTime.Now));
            // zboziList.Add(new NovyZbozi("465DFSS", "lyze", 80, DateTime.Now, "3", 2, DateTime.Now));
            AddUserLib.NovaZakazkaEventArgs zakaznik = new AddUserLib.NovaZakazkaEventArgs("213213OCisloKarty", "David", "Roško", "920219", zboziList, 1000);
            Print(zakaznik);
        }

        /// <summary>
        /// Vytiskne účtenku podle
        /// </summary>
        /// <param name="table"></param>
        public static void Print(AddUserLib.NovaZakazkaEventArgs zakaznik)
        {
            string buffer = "\x1b\x1d\x61\x1";             //Center Alignment - Refer to Pg. 3-29
            buffer = buffer + "\x1B\x1C\x70\x1\x0";          //Stored Logo Printing - Refer to Pg. 3-38
            buffer = buffer + "Katedra tělesné výchovy a sportu\n";
            buffer = buffer + "Půjčovna sportovních potřeb\n";
            buffer = buffer + "Univerzitní 8 Plzeň 306 14\n\n";
            buffer = buffer + "\x1b\x1d\x61\x0";             //Left Alignment - Refer to Pg. 3-29
            buffer = buffer + "\x1b\x44\x2\x10\x22\x0";      //Setting Horizontal Tab - Pg. 3-27
            buffer = buffer + "Datum: " + DateTime.Now.ToShortDateString() + "\x9" + " Čas: " + DateTime.Now.ToShortTimeString() + "\n";      //Moving Horizontal Tab - Pg. 3-26
            buffer = buffer + "------------------------------------------------ \n";
            buffer = buffer + "\n";        
            buffer = buffer + "\x1b\x45";                    //Select Emphasized Printing - Pg. 3-14
            buffer = buffer + "Příjemce:\n";
            buffer = buffer + "\x1b\x46";                    //Cancel Emphasized Printing - Pg. 3-14
            if(zakaznik.CisloKarty != "")
                buffer = buffer + zakaznik.Jmeno + "\x9" + zakaznik.Prijmeni + "   ČK: " + zakaznik.CisloKarty + "\n";
            else
                buffer = buffer + zakaznik.Jmeno + "\x9" + zakaznik.Prijmeni  + "   OP: " + zakaznik.CisloOP + "\n";
            buffer = buffer + "\n";
            buffer = buffer + "\x1b\x45";                    //Select Emphasized Printing - Pg. 3-14
            buffer = buffer + "Půjčeno\n";
            buffer = buffer + "\x1b\x46";                    //Cancel Emphasized Printing - Pg. 3-14

            foreach (NovyZbozi zbozi in zakaznik.SeznamZbozi)
            {
              //  buffer = buffer + zbozi.KeDni.ToShortDateString() + "\x9" + "  " + zbozi.Nazev + "\x9" + "         " + zbozi.Cena + "\n";
                buffer = buffer + zbozi.KeDni.ToShortDateString() + "\x9" + "  " + zbozi.Nazev + "(" + zbozi.CarovyKod + ")\x9" + "         " + zbozi.Cena + ",-\n";
            }
            buffer = buffer + "\n";
            buffer = buffer + "------------------------------------------------ \n";
            buffer = buffer + "Celkem" + "\x6" + "" + "\x9" + "\x1b\x69\x1\x1" + "         " + zakaznik.CenaCelkem + "\n";    //Character Expansion - Pg. 3-10
            buffer = buffer + "\x1b\x69\x0\x0";                                                          //Cancel Expansion - Pg. 3-10
            buffer = buffer + "------------------------------------------------ \n";
            buffer = buffer + "\x1b\x34" + "Převzetím souhlasím se smluvními podmínkami" + "\x1b\x35\n";                       //Specify/Cencel White/Black Invert - Pg. 3-16
            buffer = buffer + "Within " + "\x1b\x2d\x1" + "30 days" + "\x1b\x2d\x0" + " with receipt\n"; //Specify/Cancel Underline Printing - Pg. 3-15
            buffer = buffer + "\x1b\x64\x02";                                           //Cut  - Pg. 3-41
            buffer = buffer + "\x7";                                                 //Open Cash Drawer

            PrintDialog pDialog = new PrintDialog();

            pDialog.PrinterSettings = new PrinterSettings();
            pDialog.PrinterSettings.PrinterName = GetPrinterName();
            pDialog.PrinterSettings = pDialog.PrinterSettings;
            RawPrinterHelper.SendStringToPrinter(pDialog.PrinterSettings.PrinterName, buffer);
            
        }

        public static string GetPrinterName()
        {
            if (Properties.Settings.Default.PrinterName == "")          //Prvni tisknuti
            {
                PrinterSettings settings = new PrinterSettings();
                foreach (string printer in PrinterSettings.InstalledPrinters)
                {
                    settings.PrinterName = printer;
                    if (settings.IsDefaultPrinter)
                    {
                        Properties.Settings.Default.PrinterName = printer;
                        Properties.Settings.Default.Save();
                        return printer;
                    }
                }
                return string.Empty;
            }

            return Properties.Settings.Default.PrinterName;
        }
    }
}
